<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\User;

class DictionaryController extends BaseController
{
    /**
     * 字典信息接口
     * by 吉星
     */
    // 字典信息数据
    public function dictionary(Request $request){
        if($request){
            $where = [];
            if($request['name']){
                $where['name'] = $request['name'];
            }
            if ($request['starttime'] || $request['endtime']) {
                $starttime = strtotime($request['starttime']);
                $endtime = strtotime($request['endtime']);
                $data = Db::table('dictionary')
                    ->where($where)
                    ->where('is_delete',1)
                    ->whereBetween('update_time',[$starttime,$endtime])
                    ->get()
                    ->toArray();
            }else{
                $data = Db::table('dictionary')
                    ->where($where)
                    ->where('is_delete',1)
                    ->get()
                    ->toArray();
            }
            // $data = Db::table('dictionary')
            //         ->where($where)
            //         ->get()
            //         ->toArray();
            $data = json_decode(json_encode($data),true);
            // dd($data);
            foreach ($data as $key => $value) {
                $data[$key]['arr'] = json_decode($data[$key]['contents'],true);
                // $data[$key]['contentss'] = implode(',', $data[$key]['arr']).';';
                // dd($data);
                $data[$key]['contentss'] = '';
                for ($i=0; $i < count($data[$key]['arr']); $i++) { 
                    $data[$key]['contentss'] .= implode(',', $data[$key]['arr'][$i]).';';
                }
            }
            // dd($data);
            if($data){
                $res = ['code'=>1000,'message'=>'获取数据成功!','data'=>$data,'where'=>$where];
            }else{
                $res = ['code'=>1002,'message'=>'没有找到符合条件的数据!','where'=>$where];
            }
        }else{
            $res = ['code'=>1001,'message'=>'参数错误!'];
        }
        
        return $res;
    }
    // 修改字典信息页面赋值
    public function edit(Request $request){
        if($request['id']){
            $data = Db::table('dictionary')->where('id',$request['id'])->first();
            // dump($data);
            $data = json_decode(json_encode($data),true);
            $data['arr'] = json_decode($data['contents'],true);
            // print_r($data['arr']);
            $data['contentss'] = '';
            for ($i=0; $i < count($data['arr']); $i++) { 
            	$data['contentss'] .= implode(',', $data['arr'][$i]).';';
            }
            // dump($data['str']);
            // dd($data);
            return ['data'=>$data,'code'=>1000,'message'=>'获取数据成功'];
        }else{
            return ['code'=>1001,'message'=>'请传入字典配置ID'];
        }
    }
    // 增加字典信息
    public function convoy_add(Request $request){
        if($request){
            $data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
            $data['update_time'] = date('Y-m-d H:i;s',time());
            $arr = $request['contents'];
            for ($i=0; $i < count($arr); $i++) {
                $arr[$i] = explode(':', $arr[$i]);
                $newarr[$i]['name'] = $arr[$i][0];
                $newarr[$i]['value'] = $arr[$i][1];
            }
            $data['contents'] = json_encode($newarr);
            $save = Db::table('dictionary')->insert($data);
            if($save){
                $res = ['code'=>1000,'message'=>'增加成功!'];
            }else{
                $res = ['code'=>1001,'message'=>'增加失败,请重试!'];
            }
        }else{
            $res = ['code'=>1002,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 修改字典信息
    public function convoy_edit(Request $request){
        if($request){
            $data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
            if($data['id']){
                $data['update_time'] = date('Y-m-d H:i;s',time());
                $arr = $request['contents'];
                for ($i=0; $i < count($arr); $i++) { 
                    $arr[$i] = explode(':', $arr[$i]);
                    $newarr[$i]['name'] = $arr[$i][0];
                    $newarr[$i]['value'] = $arr[$i][1];
                }
            	$data['contents'] = json_encode($newarr);
                $save = Db::table('dictionary')->where('id','=',$data['id'])->update($data);
                if($save){
                    $res = ['code'=>1000,'message'=>'修改成功!'];
                }else{
                    $res = ['code'=>1001,'message'=>'修改失败,没有数据发生变化!'];
                }
            }else{
                $res = ['code'=>1002,'message'=>'修改失败,没有找到符合条件的数据!'];
            }
        }else{
            $res = ['code'=>1003,'message'=>'参数错误!'];
        }
        return $res;
    }

    // 删除字典 软删除
    public function del(Request $request){
    	if($request['id']){
    		$data['is_delete'] = 2;
    		$data['update_time'] = date('Y-m-d H:i;s',time());
    		// $data['save_userid'] = session('uid');
    		$del = Db::table('dictionary')->whereIn('id',$request['id'])->update($data);
    		if($del){
    			$res = ['code'=>1000,'message'=>'删除成功!'];
    		}else{
    			$res = ['code'=>1002,'message'=>'删除失败,请确认该数据是否已经删除!'];
    		}
    	}else{
    		$res = ['code'=>1001,'message'=>'未找到要删除的数据!'];
    	}
    	return $res;
    }

    /**
     * [dictionary 字典列表]
     * @Author   孙丽男
     * @DateTime 2018-06-14
     * @param    [type]
     * @version  6.2
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function dictionaryList(Request $request){
        if($request['id']){
            $id = $request['id'];
            // 获取对应的字典信息
            $zidian = Db::table('dictionary')
                ->where('id',$id)
                ->first();
            $zidian = json_decode(json_encode($zidian),true);

            // print_r(json_decode($zidian['contents'],true));exit;
            $data = json_decode($zidian['contents'],true);
            $res = ['code'=>1000,'message'=>'成功','data'=>$data];
        }else{
            $res = ['code'=>1001,'message'=>'参数错误!'];
        }
        
        return $res;
    }
}
